package com.tgh.au.c5;

import java.util.concurrent.TimeUnit;

import com.tgh.au.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;

public class GameView8 extends View implements Runnable {
	private Matrix matrix=new Matrix();
	Bitmap qq;
	int qqWidth;
	int qqHeight;
	float scale=1.0f;
	private Paint paint;
	public GameView8(Context context) {
		super(context);
		setFocusable(true);
		requestFocus();
		qq=BitmapFactory.decodeResource(getResources(), R.drawable.qq);
		qqWidth=qq.getWidth();
		qqHeight=qq.getHeight();
		paint = new Paint();
		new Thread(this).start();
	}
	@Override
	protected void onDraw(Canvas canvas) {
		matrix.reset();
		matrix.postScale(scale, scale);
		paint.setColor(Color.RED);
		canvas.drawOval(new RectF(20, 20, 70, 120), paint);
		Bitmap bitmap = Bitmap.createBitmap(qq, 0, 0, qqWidth, qqHeight, matrix, true);
		canvas.drawBitmap(bitmap, 120, 200, null);
		bitmap.recycle();
		bitmap=null;
	}
	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			try {
				TimeUnit.MILLISECONDS.sleep(100);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			postInvalidate();
		}
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			if (scale>0.3) {
				scale-=0.1;
			}
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			if (scale<1.5) {
				scale+=0.1;
			}
			break;
		}
		return true;
	}

}
